package core.field;

import core.Player;
import core.PopUpBuy;

public class Fleet extends Ownable {
	
	private final int RENT_1 = 500;
	private final int RENT_2 = 1000;
	private final int RENT_3 = 2000;
	private final int RENT_4 = 4000;
	
	
	
	public Fleet(String name){
		this.setName(name);
		this.setPrice(4000);
	}
	
	public Fleet(String name, int price){
		this.setName(name);
		this.setPrice(price);
	}
	
	public Fleet(){
		this.setPrice(4000);
	}
	
	

	@Override
	public void landOnField(Player player) {
		if(player != this.owner && this.owner != null){
			player.getKonto().withdrawPoints(getRent());
			owner.getKonto().addPoints(getRent());
		}
		if(this.owner == null){
			if(new PopUpBuy().BuyFieldQ())
				this.buyField(player);
		}
		
				
	}
	

	@Override
	public int getRent() {
		if(countFleets(this.owner)==1)
			return this.RENT_1;
		else if(countFleets(this.owner)==2)
			return this.RENT_2;
		else if(countFleets(this.owner)==3)
			return this.RENT_3;
		else if(countFleets(this.owner)==4)
			return this.RENT_4;
		else
			return 0;
	}
	
	private int countFleets(Player p){
		int temp=0;
		
		if(p == null){
			return 0;
		}
		
		for(int i =0 ; i< p.returnFields().length; i++){
			
			if(p.returnFields()[i].getClass().equals(new Fleet().getClass())){
				temp++;
			}
		}
		
		return temp;
	}

	@Override
	public String subText() {
		String temp = ".";
		if(owner != null){
			temp = owner.getName();
			temp += String.valueOf(getRent());
		}
		else
			temp = String.valueOf(price);
		return temp;
	}
	
	

}
